package com.cupcorn.lib.display;

import java.io.IOException;

import org.andengine.entity.scene.Scene;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.cupcorn.lib.Utils;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameTiledSprite;
import com.cupcorn.lib.game.element.GameElementTiledSprite;

public class DisplayTiledSprite
{
	GameEngine					engine;
	GameElementTiledSprite[]	gameElementTiledSprite;
	TiledTextureRegion[]		textureRegion;
	Bitmap						bitmap;
	
	public DisplayTiledSprite(GameEngine engine, GameElementTiledSprite[] gameElementTiledSprite)
	{
		this.engine = engine;
		this.gameElementTiledSprite = gameElementTiledSprite;
		textureRegion = new TiledTextureRegion[gameElementTiledSprite.length];
		for (int i = 0; i < gameElementTiledSprite.length; i++)
		{
			try
			{
				bitmap = BitmapFactory.decodeStream(engine.getAssets().open(gameElementTiledSprite[i].path));
				
				BitmapTextureAtlas gameSpriteITexture = new BitmapTextureAtlas(engine.getTextureManager(), bitmap.getWidth(), bitmap.getHeight());
				
				gameSpriteITexture.load();
				textureRegion[i] =
					BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(gameSpriteITexture, engine, gameElementTiledSprite[i].path, 0, 0,
						gameElementTiledSprite[i].col, gameElementTiledSprite[i].row);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			
		}
	}
	
	public GameTiledSprite newTiledSprite(int index)
	{
		return newTiledSprite(index, engine.getEngine().getScene());
	}
	
	public GameTiledSprite newTiledSprite(int index, Scene scene)
	{
		GameTiledSprite sprite = tiledSprite(index);
		scene.attachChild(sprite);
		return sprite;
	}
	
	public GameTiledSprite tiledSprite(int index)
	{
		float width = 0;
		float height = 0;
		
		if (gameElementTiledSprite[index].width != 0 && gameElementTiledSprite[index].height != 0)
		{
			width = Utils.getRatioWidth(gameElementTiledSprite[index].width, gameElementTiledSprite[index].ratio);
			height = Utils.getRatioHeight(gameElementTiledSprite[index].height, gameElementTiledSprite[index].ratio);
		}
		else
		{
			width = Utils.getRatioWidth(bitmap.getWidth() / gameElementTiledSprite[index].col, gameElementTiledSprite[index].ratio);
			height = Utils.getRatioHeight(bitmap.getHeight() / gameElementTiledSprite[index].row, gameElementTiledSprite[index].ratio);
			
		}
		float x = Utils.getPosX(gameElementTiledSprite[index].x, width, gameElementTiledSprite[index].anchor, gameElementTiledSprite[index].ratio);
		float y = Utils.getPosY(gameElementTiledSprite[index].y, height, gameElementTiledSprite[index].anchor, gameElementTiledSprite[index].ratio);
		return new GameTiledSprite(x, y, width, height, textureRegion[index], engine.getVertexBufferObjectManager());
	}
}
